

#include "app_video.h"


#include "../public/public_msg/public_msg.h"

//*******************************************************************************************

static vplay_para_t v_p;

//*******************************************************************************************

int32_t app_video_get_filenum(void)
{

	if(!v_p.p_fl_v){
		return 0;
	}

	if(!v_p.p_fl_v->f_total){
		return 0;
	}

	return v_p.p_fl_v->f_total;
}

int32_t app_video_switch(int32_t index)
{
	int ret  = -1;

	char video_name[1024] = {0};
	
	//************************************************************************************
	memset(video_name, 0, sizeof(video_name));
	//************************************************************************************

	v_p.p_err_id = 0;
	//************************************************************************************

	ret  = filelist_get_filename(v_p.p_fl_v,video_name,index);
	if((ret != 0)||(strlen(video_name) <= 0))
	{	
		log_e("ret =%d\n",ret);

		v_p.p_err_id = 1;

		return -1;
	}
	else
	{

	}
	//************************************************************************************
	log_i("i:t:n=%d:%d:%s\n",index,app_video_get_filenum(),video_name);
	//************************************************************************************
	ffmpeg_stop_file();
	ffmpeg_decode_file(video_name);
	//************************************************************************************

	video_para_set_playindex(v_p.p_index);
	system_para_save();
	//************************************************************************************

	return 0;
	
}


int32_t app_video_switch_prev(void)
{
	
	if(v_p.p_index <= 0){
		v_p.p_index = app_video_get_filenum() - 1; 
	}else{
		v_p.p_index -- ;
	}

	app_video_switch(v_p.p_index);

	return 0;
}


int32_t app_video_switch_next(void)
{
	v_p.p_index ++ ;
	if(v_p.p_index >= app_video_get_filenum()){
		v_p.p_index = 0; 
	}

	log_d("p_index =%d\n",v_p.p_index);

	app_video_switch(v_p.p_index);

	return 0;
}




int32_t app_video_error_switch(void)
{
	if(!v_p.p_err_id)
		return -1;

	if(v_p.p_err_id == 1)
	{
		app_video_switch_next();
	}
	

	v_p.p_err_id = 0;

	return 0;
}

void* app_video_msg_task(void *arg)
{
	int ret = -1;
	
	unsigned int cmd    = 0;
	unsigned int data0  = 0;
	unsigned int data1  = 0;
	
	struct timeval timer;

	while(1)
	{
		usleep(1000);

		timer.tv_sec  = 1;
		timer.tv_usec = 0;

		switch(select(0, NULL, NULL, NULL, &timer)) 
		{
		case -1:  
			log_e("Error!\n");
			break;
		case 0: 
			break;
		default:break;
		}
		//***********************************************

		ret = message_packet_recv(&cmd,&data0,&data1);
		if(ret != 0){
			continue;
		}

		switch(cmd){
		case MSG_VIDEO_END:
		{
			app_video_switch_next();

			break;
		}		
		case MSG_KEY_LEFT:
		{
			app_video_switch_prev();

			break;
		}	
		case MSG_KEY_RIGHT:
		{
			app_video_switch_next();

			break;
		}
		default:break;
		}
		//***********************************************

	}

}

void* app_video_autoplay_task(void *arg)
{
	while(1)
	{
		usleep(1000);

		//***********************************************
		
		app_video_error_switch();
		//***********************************************

	}

}
//*******************************************************************************************

int32_t app_video_create(void)
{
	
	int  ret  = -1;

	char search_path[1024];
	//********************************************************************************
	if(!video_para_get_is_show())
		return -1;

	srand(time(NULL));
	//********************************************************************************
	log_i(" video app create\n");
	//********************************************************************************

	#if 0
	memset(search_path,0,sizeof(search_path));
	ret = video_para_get_path(search_path);
	if(ret != 0){
		sprintf(search_path,"%s",CFG_VIDEO_PATH);
	}
	#else 
	
	sprintf(search_path,"%s",CFG_VIDEO_PATH);
	
	#endif

	log_i("search_path:%s\n",search_path);

	v_p.p_err_id  = 0;
	//********************************************************************************

	v_p.p_fl_v = filelist_cjson_create(search_path,FILE_TYPE_VIDEO,"video_file.tmp");
	if(!v_p.p_fl_v){
		log_e("create filelist error\n");
		return -1;
	}
	//********************************************************************************
	v_p.p_index   = rand()%app_video_get_filenum();
	
	app_video_switch(v_p.p_index);
	//********************************************************************************

	ret=pthread_create(&v_p.p_v_thread, NULL, app_video_msg_task, NULL);
	if(ret != 0){
		log_e("error=%s\n",strerror(ret));
	}

	ret=pthread_create(&v_p.p_msg_thread, NULL, app_video_autoplay_task, NULL);
	if(ret != 0){
		log_e("error=%s\n",strerror(ret));
	}
	//********************************************************************************

	return 0;
}

int32_t app_video_destory(void)
{

	if(v_p.p_fl_v){
		filelist_cjson_destory(v_p.p_fl_v);
		v_p.p_fl_v = NULL;
	}

	log_i("video exit end\n");
		

	return 0;
}
